/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.ReservaSalaDao;
import edu.upc.clase.demo.entity.ReservaSala;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;
/**
 *
 *  private String codResSala;
    private Date fechaResSala;
    private Date horaResSala;
    private Double costoResSala;
    private Integer id; 
    private String codSala;
 */
@Repository
public class ReservaSalaDaoImpl extends SimpleJdbcDaoSupport implements ReservaSalaDao{
  private static Logger log = LoggerFactory.getLogger(ReservaSalaDaoImpl.class);
  
      @Autowired
       public ReservaSalaDaoImpl (DataSource dataSource) {
       log.info("Asignando el dataSource");
       setDataSource(dataSource);    
    }
      @Override
      public Integer insertar(ReservaSala reservaSala) {
        getJdbcTemplate().update(
                "insert into Local (codResSala,fechaResSala,horaResSala,"
                + "costoResSala,id,codSala) values (?, ?, ?, ?, ?, ?)",
                reservaSala.getCodResSala(),reservaSala.getFechaResSala(),
                reservaSala.getHoraResSala(),reservaSala.getCostoResSala(),
                reservaSala.getId(),reservaSala.getCodSala());
        return getSimpleJdbcTemplate().queryForInt("select last_insert_codResSala()");
    }
      @Override
      public void actualizar(ReservaSala reservaSala) {
        getJdbcTemplate().update(
                "update Local set fechaResSala = ?, horaResSala = ?,"
                + "costoResSala = ?,id = ?,codSala = ? where codResSala = ?",
                reservaSala.getFechaResSala(),reservaSala.getHoraResSala(),
                reservaSala.getCostoResSala(),reservaSala.getId(),
                reservaSala.getCodSala(),reservaSala.getCodResSala());
    }

    @Override
    public void eliminar(ReservaSala reservaSala) {
        getJdbcTemplate().update(
                "delete from Local where codResSala = ?",reservaSala.getCodResSala() );
    }
    @Override
    public ReservaSala buscar(String fechaResSala) {
        try {
            return getSimpleJdbcTemplate().queryForObject(
                    "select codResSala,fechaResSala,horaResSala,"
                    + "costoResSala,id,codSala from Local from "
                    + "Local where fechaResSala=?",
                    new BeanPropertyRowMapper<ReservaSala>(ReservaSala.class), fechaResSala);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<ReservaSala> buscarTodos() {
        return getSimpleJdbcTemplate().query(
                "select codResSala,fechaResSala,horaResSala,"
                + "costoResSala,id,codSala from Local",
                new BeanPropertyRowMapper<ReservaSala>(ReservaSala.class));
    }

    @Override
    public Integer insertar(ReservaSalaDao reservaSala) {
        throw new UnsupportedOperationException("Not supported yet.");
    }


}
